package com.nbjtjc.safe.activiti.api;

import com.nbjtjc.safe.activiti.model.ActivitiParams;
import com.nbjtjc.safe.activiti.model.ExtActNode;
import com.nbjtjc.safe.activiti.service.ActivitiService;
import com.nbjtjc.safe.activiti.service.EmgFlowService;
import com.nbjtjc.safe.annotation.ReassignTaskCheck;
import com.nbjtjc.safe.annotation.SysLog;
import com.nbjtjc.safe.bean.Page;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.model.emg.EmgPlanPublic;
import org.activiti.engine.RepositoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @auther guqianbin
 * 应急处置的工作流接口
 * @date 2018/5/21 21:13
 **/
@RestController
@RequestMapping("/emgFlow")
@CrossOrigin("*")
public class FlowEmgController {

    @Autowired
    private EmgFlowService emgFlowService;

    @Autowired
    private ActivitiService activitiService;
    @GetMapping("/getEmgFlowStatus")
    public Result getEmgFlowStatus(){
        Result result=Result.res();
        result.setData(emgFlowService.getEmgFlowStatus());
        return result;
    }


    @PostMapping("/startPlanPublic")
    public Result startEmg(EmgPlanPublic emgPlanPublic, int applyUserId) throws Exception {
        Result result = new Result();
        result = emgFlowService.startEmgFlow(emgPlanPublic, applyUserId);
        return result;
    }

    @PostMapping("/getMyTask")
    public Result myEmgTask(Page page,String taskDefineKey) {
        Result result = new Result();
        result=emgFlowService.getUndoEmgTasks(page,taskDefineKey);
        return result;
    }

    @PostMapping("/doTask")
    @SysLog("/执行工作流任务")
    @ReassignTaskCheck
    public Result doTask(ActivitiParams activitiParams) {
        Result result = new Result();
        result = emgFlowService.finishTask(activitiParams);
        return result;
    }

    /**
     * 获取实时流程图
     * @param processInstanceId 流程实例
     * @return
     */
    @GetMapping("/showFlowImg/{processInstanceId}")
    public Result showFlowImg(@PathVariable("processInstanceId") String processInstanceId, HttpServletResponse response) throws Exception {
        try {
            InputStream inputStream = emgFlowService.getFlowImgByInstantId(processInstanceId);
            //输出到页面
            byte[] b = new byte[1024];
            int len;
            while ((len = inputStream.read(b, 0, 1024)) != -1) {
                response.getOutputStream().write(b, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @GetMapping("/getProcessHistoryTask/{processInstanceId}")
    public Result getProcessHistoryTask(@PathVariable("processInstanceId") String processInstanceId){
        Result result=new Result();
        result.setData(activitiService.getHistoryTask(processInstanceId));
        return result;
    }

    @PostMapping("/getMyEmgHistoryTask")
    public Result getMyEmgHistoryTask(Page page){
        Result result=new Result();
        result.setData(emgFlowService.getMyEmgHistoryTask(page));
        return result;
    }
}
